package com.cdxy.dmr.repository;

import com.cdxy.dmr.dataObject.PayOrderDO;
import com.cdxy.dmr.mapper.PayOrderMapper;
import com.cdxy.enums.OrderEnum;
import com.cdxy.model.dto.base.mybatis.BaseRepository;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: tangy
 * @Date: 2020/4/7 18:03
 */
@Repository
public class PayOrderRepository extends BaseRepository<PayOrderMapper, PayOrderDO> {
    /**
     * 根据订单号查询
     *
     * @param orderNo
     * @param payType
     * @return
     */
    public PayOrderDO getByOrderNoAndType(String orderNo, OrderEnum.PayType payType) {
        return this.baseMapper.selectOne(this.queryWrapper()
                .eq(PayOrderDO::getBizNo, orderNo)
                .eq(PayOrderDO::getType, payType.name())
                .last("LIMIT 1"));
    }

    /**
     * 更新过期时间
     *
     * @return
     */
    public List<PayOrderDO> listByValidAndStatus() {
        return this.baseMapper.selectList(this.queryWrapper()
                .eq(PayOrderDO::getStatus, OrderEnum.OrderStatus.PAYING.name())
                .lt(PayOrderDO::getValidTime, LocalDateTime.now()));
    }

    public void updateStatusByIdBatch(List<Integer> ids, OrderEnum.OrderStatus status) {
        this.baseMapper.updateStatusByIdBatch(ids, status.name());
    }

    /**
     * 更新订单状态
     *
     * @param newStatus
     * @param bizNo
     * @param curStatus
     * @return
     */
    public boolean updateStatusByOrderNoAndOldStatus(OrderEnum.OrderStatus newStatus, String bizNo, OrderEnum.OrderStatus curStatus) {
        return this.baseMapper.updateStatusByOrderNoAndOldStatus(newStatus.name(), bizNo, curStatus.name());
    }
}
